﻿using System;
using System.IO;

namespace Game.Lib.ztPackets
{
    public class LoginResponsePacket : Packet
    {
        /// <summary>
        /// URI of the sim to login to, or "" if login request failed
        /// </summary>
        public string LoginURI;

        /// <summary>
        /// Server to connect to to initiate group chats and IMs
        /// </summary>
        public string ChatURI;

        /// <summary>
        /// Reason login failed, or "" if succeded
        /// </summary>
        public string Reason;

        /// <summary>
        /// UUID of the agent this session is tied to
        /// </summary>
        public UUID AgentID;

        /// <summary>
        /// UUID of this session
        /// </summary>
        public UUID SessionID;

        /// <summary>
        /// ID of the region the agent starts in
        /// </summary>
        public UUID RegionID;

        /// <summary>
        /// Initial location of the agent
        /// </summary>
        public Vector3 Location;

        /// <summary>
        /// Initial rotation of the agent
        /// </summary>
        public Quaternion Rotation;

        /// <summary>
        /// UUID used to request the agent's inventory
        /// </summary>
        public UUID InventoryRoot;

        public LoginResponsePacket()
        {

        }

        public override int Length
        {
            get
            {
                return 4 + Utils.UTF8.GetByteCount(LoginURI) + //LoginURI
                    4 + Utils.UTF8.GetByteCount(ChatURI) + //ChatURI
                    4 + Utils.UTF8.GetByteCount(Reason) + //Reason
                    16 + //AgentID
                    16 + //SessionID
                    16 + //RegionID
                    12 + //Location
                    16 + //Rotation
                    16;  //InventoryRoot
            }
        }

        public override byte[] ToBytes()
        {
            MemoryStream ms = new MemoryStream(Length);

            ms.Write(Utils.GetBytes(LoginURI), 0, Utils.UTF8.GetByteCount(LoginURI) + 4);
            ms.Write(Utils.GetBytes(ChatURI), 0, Utils.UTF8.GetByteCount(ChatURI) + 4);
            ms.Write(Utils.GetBytes(Reason), 0, Utils.UTF8.GetByteCount(Reason) + 4);
            AgentID.Serialize(ms);
            SessionID.Serialize(ms);
            RegionID.Serialize(ms);
            Location.Serialize(ms);
            Rotation.Serialize(ms);
            InventoryRoot.Serialize(ms);

            return GetTotalBuffer(ms.ToArray());
        }

        public override void FromBytes(byte[] data, int pos)
        {
            throw new System.NotImplementedException();
        }
    }
}
